"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _jsxRuntime = require("react/jsx-runtime");

var _icons = require("@ant-design/icons");

var _antdV = require("antd-v4");

var _react = _interopRequireWildcard(require("react"));

var _createField = require("../../BaseForm/createField");

var _excluded = ["fieldProps", "name", "action", "accept", "listType", "title", "max", "icon", "buttonProps", "onChange", "disabled", "proFieldProps"];

/**
 * 上传按钮组件
 *
 * @param
 */
var BaseProFormUploadButton = function BaseProFormUploadButton(_ref, ref) {
  var fieldProps = _ref.fieldProps,
      name = _ref.name,
      action = _ref.action,
      accept = _ref.accept,
      listType = _ref.listType,
      _ref$title = _ref.title,
      title = _ref$title === void 0 ? '单击上传' : _ref$title,
      max = _ref.max,
      _ref$icon = _ref.icon,
      icon = _ref$icon === void 0 ? (0, _jsxRuntime.jsx)(_icons.UploadOutlined, {}) : _ref$icon,
      buttonProps = _ref.buttonProps,
      _onChange = _ref.onChange,
      disabled = _ref.disabled,
      proFieldProps = _ref.proFieldProps,
      restProps = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var value = (0, _react.useMemo)(function () {
    var _restProps$fileList;

    return (_restProps$fileList = restProps.fileList) !== null && _restProps$fileList !== void 0 ? _restProps$fileList : restProps.value;
  }, [restProps.fileList, restProps.value]); // 如果配置了 max ，并且 超过了文件列表的大小，就不展示按钮

  var showUploadButton = (max === undefined || !value || (value === null || value === void 0 ? void 0 : value.length) < max) && (proFieldProps === null || proFieldProps === void 0 ? void 0 : proFieldProps.mode) !== 'read';
  var isPictureCard = (listType !== null && listType !== void 0 ? listType : fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.listType) === 'picture-card';
  return (0, _jsxRuntime.jsx)(_antdV.Upload, (0, _objectSpread2.default)((0, _objectSpread2.default)({
    action: action,
    accept: accept,
    ref: ref,
    // 'fileList' 改成和 ant.design 文档中 Update 组件 默认 file字段一样
    name: name || 'file',
    listType: listType || 'picture',
    fileList: value
  }, fieldProps), {}, {
    onChange: function onChange(info) {
      var _fieldProps$onChange;

      _onChange === null || _onChange === void 0 ? void 0 : _onChange(info);
      fieldProps === null || fieldProps === void 0 ? void 0 : (_fieldProps$onChange = fieldProps.onChange) === null || _fieldProps$onChange === void 0 ? void 0 : _fieldProps$onChange.call(fieldProps, info);
    },
    children: showUploadButton && (isPictureCard ? (0, _jsxRuntime.jsxs)("span", {
      children: [icon, " ", title]
    }) : (0, _jsxRuntime.jsxs)(_antdV.Button, (0, _objectSpread2.default)((0, _objectSpread2.default)({
      disabled: disabled || (fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.disabled)
    }, buttonProps), {}, {
      children: [icon, title]
    })))
  }));
};

var ProFormUploadButton = (0, _createField.createField)( /*#__PURE__*/_react.default.forwardRef(BaseProFormUploadButton), {
  getValueFromEvent: function getValueFromEvent(value) {
    return value.fileList;
  }
});
var _default = ProFormUploadButton;
exports.default = _default;